<?php


namespace app\Utils;


class StrUtil
{
    ## 余额
    const BILL_USE_TYPE = [
        'WITHDRAWAL'        => 1,#提现,
        'VIP_COMMISSION'    => 3,#邀请用户奖励
        'INVITE_REWARD'     => 5,#推荐赏金
        'GOODS_COMMISSION'  => 7,#商品佣金
        'COURSE_COMMISSION' => 9,#课程佣金
        'TUI_GOODS'         => 11,#退回
        'OFFLINE_USE'       => 2,#线下使用
        'COLLAGE_WAY'       => 12,#拼团失败退款余额
    ];
    ## 学习金
    const STUDY_BILL_USE_TYPE = [
        'BUY_VIP'         => 1,#开通会员奖励,
        'OFFLINE_USE'     => 2,#线下使用
        'BUY_GOODS_USE'   => 8,#购买商品使用
        'TUI_GOODS'       => 3,#退回
    ];
    ## 积分
    const INTEGRAL_BILL_USE_TYPE = [
        'BUY_VIP'         => 5,#购买会员领取,
        'BUY_GOODS_AWARD' => 1,#购买商品奖励,
        'OFFLINE_USE'     => 2,#线下使用
        'BUY_GOODS_USE'   => 8,#购买商品使用
        'TUI_GOODS'       => 3,#退回
    ];

    ## 商家余额
    const MER_BILL_USE_TYPE = [
        'WITHDRAWAL'     => 1,#提现,
        'SELL_GOODS'     => 5,#售卖商品
        'SELL_COURSE'    => 10,#售卖课程
    ];

    public static function i()
    {
        return new StrUtil();
    }

    /**
     * 中文名字处理*
     * @param $str
     * @param int $type
     * @return string
     */
    public function strRealName($str,$type = 0)
    {
        $strlen   = mb_strlen($str, 'utf-8');
        //第一个汉字
        $firstStr = mb_substr($str, 0, 1, 'utf-8');
        //最后一个汉字
        $lastStr = mb_substr($str, -1, 1, 'utf-8');
        if(!empty($type)){
            if($strlen == 2){
                $str = $firstStr.str_repeat('*',mb_strlen($str, 'utf-8') - 2).$lastStr;
            }
            if($strlen > 2){
                $str = $firstStr."*".$lastStr;
            }
            if($strlen < 2){
                $str = $firstStr."*";
            }
        }else{
            if($strlen >= 2){
                $str = $firstStr."**";
            }
            if($strlen < 2){
                $str = $firstStr."*";
            }
        }
        return $str;
    }

    /**
     * 电话号码处理*
     * @param $str
     * @param int $type
     * @return mixed
     */
    public function strMobile($str,$type = 0)
    {
        if(!empty($type)){
            $str = substr_replace($str, '****',3, 4);
        }else{
            $str = substr_replace($str, '********',3, strlen($str)-3);
        }
        return $str;
    }

    /**
     * 订单号
     * @param int $type
     * @return string
     */
    public function getOrderSn($type=0)
    {
        $order_sn = "";
        if(!empty($type)){
            if($type == 1){
                //优惠订单
                $order_sn = "YH_".date("YmdHis",time()).str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            }
            if($type == 2){
                //拼团订单
                $order_sn = "PT_".date("YmdHis",time()).str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            }
            if($type == 3){
                //商户订单号
                $order_sn = "SH_".date("YmdHis",time()).str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            }
            if($type == 6){
                //免费领取订单号
                $order_sn = "RF_".date("YmdHis",time()).str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            }
        }else{
            //普通订单
            $order_sn = "MF_".date("YmdHis",time()).str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        }
        return $order_sn;
    }

}